﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BeeCoders.Tools.Wpf.FileExplorer
{
	/// <summary>
	/// Interaction logic for FileExplorer.xaml
	/// </summary>
	public partial class FileExplorer 
	{
		public FileExplorer()
		{
			InitializeComponent();

			DataContextChanged += FileExplorerDataContextChanged;
		}

		void FileExplorerDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			var vm = DataContext as FileExplorerViewModel;

			if (vm != null)
			{
				vm.IsMultiSelect = IsMultiSelect;
				vm.ShowDirectories = ShowDirectories;
				vm.ShowFiles = ShowFiles;
				vm.ShowIcons = ShowIcons;
				vm.ShowRealIcons = ShowRealIcons;
				vm.IsFtpExplorer = IsFtpExplorer;
				vm.Initialize();

			}
		}

		#region Expand, Collapse
		private void ExpandAllNodes(bool expand)
		{
			foreach (object item in directoryView.Items)
			{
				var treeItem = directoryView.ItemContainerGenerator.ContainerFromItem(item) as TreeViewItem;
				if (treeItem != null)
				{
					ExpandAll(treeItem, expand);
					treeItem.IsExpanded = expand;
				}
			}
		}

		private static void ExpandAll(ItemsControl items, bool expand)
		{
			foreach (object obj in items.Items)
			{
				var childControl = items.ItemContainerGenerator.ContainerFromItem(obj) as ItemsControl;
				if (childControl != null)
				{
					ExpandAll(childControl, expand);
				}
				var item = childControl as TreeViewItem;
				if (item != null) item.IsExpanded = expand;
			}
		}
		#endregion

		#region IsMultiSelect
		public bool IsMultiSelect
		{
			get { return (bool)GetValue(IsMultiSelectProperty); }
			set
			{
				SetValue(IsMultiSelectProperty, value);

			}
		}

		public static readonly DependencyProperty IsMultiSelectProperty = DependencyProperty.Register("IsMultiSelect", typeof(bool), typeof(FileExplorer), new FrameworkPropertyMetadata(false, OnIsMultiSelectChanged));

		public void SetMultiSelect(bool isMultiSelect)
		{
			var vm = DataContext as FileExplorerViewModel;

			if (vm != null)
			{
				vm.IsMultiSelect = isMultiSelect;
			}

		}

		private static void OnIsMultiSelectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((FileExplorer)d).SetMultiSelect((bool)e.NewValue);
		}

		#endregion

		#region ShowDirectories
		public bool ShowDirectories
		{
			get { return (bool)GetValue(ShowDirectoriesProperty); }
			set
			{
				SetValue(ShowDirectoriesProperty, value);

			}
		}

		public static readonly DependencyProperty ShowDirectoriesProperty = DependencyProperty.Register("ShowDirectories", typeof(bool), typeof(FileExplorer), new FrameworkPropertyMetadata(true, OnSetShowDirectoriesChanged));

		public void SetShowDirectories(bool showDirectories)
		{
			var vm = DataContext as FileExplorerViewModel;

			if (vm != null)
			{
				vm.ShowDirectories = showDirectories;
				ExpandAllNodes(false);
			}

		}

		private static void OnSetShowDirectoriesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((FileExplorer)d).SetShowDirectories((bool)e.NewValue);
		}

		#endregion

		#region ShowFiles (for class FileExplorer)
		public bool	ShowFiles
		{
			get { return (bool)GetValue(ShowFilesProperty); }
			set
			{
				SetValue(ShowFilesProperty, value);

			}
		}

		public static readonly DependencyProperty ShowFilesProperty = DependencyProperty.Register("ShowFiles", typeof(bool), typeof(FileExplorer), new FrameworkPropertyMetadata(true, OnShowFilesChanged));

		public void SetShowFiles(bool tShowFiles)
		{
			var vm = DataContext as FileExplorerViewModel;

			if (vm != null)
			{
				vm.ShowFiles = tShowFiles;
				ExpandAllNodes(false);
			}

		}

		private static void OnShowFilesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((FileExplorer)d).SetShowFiles((bool)e.NewValue);
		}

		#endregion


		#region ShowIcons (for class FileExplorer)
		public bool ShowIcons
		{
			get { return (bool )GetValue(ShowIconsProperty); }
			set
			{
				SetValue(ShowIconsProperty, value);

			}
		}

		public static readonly DependencyProperty ShowIconsProperty = DependencyProperty.Register("ShowIcons", typeof(bool ), typeof(FileExplorer), new FrameworkPropertyMetadata(true, OnShowIconsChanged));

		public void SetShowIcons(bool  tShowIcons)
		{
			var vm = DataContext as FileExplorerViewModel;

			if (vm != null)
			{
				vm.ShowIcons = tShowIcons;
			}

		}

		private static void OnShowIconsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((FileExplorer)d).SetShowIcons((bool )e.NewValue);
		}

		#endregion


		#region ShowRealIcons (for class FileExplorer)
		public bool ShowRealIcons
		{
			get { return (bool)GetValue(ShowRealIconsProperty); }
			set
			{
				SetValue(ShowRealIconsProperty, value);

			}
		}

		public static readonly DependencyProperty ShowRealIconsProperty = DependencyProperty.Register("ShowRealIcons", typeof(bool), typeof(FileExplorer), new FrameworkPropertyMetadata(true, OnShowRealIconsChanged));

		public void SetShowRealIcons(bool tShowRealIcons)
		{
			var vm = DataContext as FileExplorerViewModel;

			if (vm != null)
			{
				vm.ShowRealIcons = tShowRealIcons;
			}

		}

		private static void OnShowRealIconsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((FileExplorer)d).SetShowRealIcons((bool)e.NewValue);
		}

		#endregion


		#region IsFtpExplorer (for class FileExplorer)
		public bool	IsFtpExplorer
		{
			get { return (bool)GetValue(IsFtpExplorerProperty); }
			set
			{
				SetValue(IsFtpExplorerProperty, value);

			}
		}

		public static readonly DependencyProperty IsFtpExplorerProperty = DependencyProperty.Register("IsFtpExplorer", typeof(bool), typeof(FileExplorer), new FrameworkPropertyMetadata(false, OnIsFtpExplorerChanged));

		public void SetIsFtpPExplorer(bool tIsFtpExplorer)
		{
			var vm = DataContext as FileExplorerViewModel;

			if (vm != null)
			{
				vm.IsFtpExplorer = tIsFtpExplorer;
			}

		}

		private static void OnIsFtpExplorerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((FileExplorer)d).SetIsFtpPExplorer((bool)e.NewValue);
		}

		#endregion

		private void CheckBox_Checked(object sender, RoutedEventArgs e)
		{

		}

		
		
		
		

		
		
	}
}
